package stud.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import stud.aistar.entity.Department;
import stud.aistar.entity.SecondDepartment;
import stud.aistar.service.IDepartmentService;
import stud.aistar.service.ISecondDepartmentService;
import stud.aistar.vo.DepartmentVo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Jcdz on 2019/10/1.
 */
@SuppressWarnings("all")
@Controller
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private ISecondDepartmentService secondDepartmentService;

    @GetMapping("/dept")
    public String department(){


        return "showmessage/department";
    }


    @GetMapping("/sdept")
    public String view(Model model){

        List<Department> departmentList = departmentService.findAll();
        List<DepartmentVo> voList = new ArrayList<>();
        Iterator<Department> iter = departmentList.iterator();
        while(iter.hasNext()){
            Department d = iter.next();
            String name = d.getDeptName();
            List<SecondDepartment> list = secondDepartmentService.findByDeptId(d.getId());
            DepartmentVo dep = new DepartmentVo(d.getId(),name,list);
            voList.add(dep);
        }

        model.addAttribute("voList",voList);


        model.addAttribute("departmentList",departmentList);

        return "showmessage/department";
    }
}
